package liuwh.mina.basic.tcp.server.tiemserver.server;


import java.util.Date;

import liuwh.mina.basic.tcp.server.tiemserver.TimeResponse;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TimeServerHandler extends IoHandlerAdapter {
	private Logger log = LoggerFactory.getLogger(TimeServerHandler.class);

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		
		String str = message.toString();
		
		log.info("server receive :" + str);
		
		if("quit".equalsIgnoreCase(str.trim())) {
			session.close(true);
			return;
		}
		
		Date date = new Date();
		
		TimeResponse res = new TimeResponse(str, date);
		
		session.write(res);
		
		log.info("message writter ...");
	}
}
